﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TodoList.Mode;
using TodoList.Common;
using System.Windows.Media;
using System.Windows.Input;
using FirstFloor.ModernUI.Presentation;
using System.ComponentModel;
using System.Linq.Expressions;
using TodoList.Command;
using System.Xml.Linq;

namespace TodoList.ViewModel
{
    public class TodoVM : INotifyPropertyChanged 
    {
        Todo todo;
        List<Todo> todoList;
       
        public TodoVM()
        {
            todo = new Todo()
            {
                CompletedStatus = (int)CompletionStatus.C0,
                Description = string.Empty,
                DueDate = DateTime.Now.ToString(),
                Priority = ((int)Priorities.Medium).ToString(),
                State = ((int)States.NotStarted).ToString(),
                Title = string.Empty
            };
            todoList = new List<Todo>();
            FileName = Utils.GetLastFile("lastFile"); // @"D:\DotNet\MetroUI\ModernUI.1.0.5\TodoList\TodoList\bin\Debug\list.xml";
            TodoList = Utils.GetTodoList(FileName);           
        }

        string fileName;
        public string FileName
        {
            get
            {
                return fileName;
            }
            set
            {
                fileName = value;
                OnPropertyChanged("FileName");
            }
        }
        public Todo Todo
        {
            get
            {
                return todo;
            }
            set
            {
                todo = value;
                OnPropertyChanged("Todo");
            }
        }
        

        public List<Todo> TodoList
        {
            get
            {
                return todoList;
            }
            set
            {
                todoList = value;
                OnPropertyChanged("TodoList");
            }
        }

        #region Action Commands
        
        SaveCommand _saveCommand;
        /// <summary>
        /// Saves changes Command
        /// </summary>
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                    _saveCommand = new SaveCommand(); // RelayCommand(param => this.SaveChanges(), param => this.CanSave);

                return _saveCommand;
            }
        }

        DeleteCommand _deleteCommand;
        /// <summary>
        /// Saves changes Command
        /// </summary>
        public ICommand DeleteAction
        {
            get
            {
                if (_deleteCommand == null)
                    _deleteCommand = new DeleteCommand(); // RelayCommand(param => this.SaveChanges(), param => this.CanSave);

                return _deleteCommand;
            }
        }

        EditCommand _editCommand;
        /// <summary>
        /// Saves changes Command
        /// </summary>
        public ICommand EditAction
        {
            get
            {
                if (_editCommand == null)
                    _editCommand = new EditCommand(); 

                return _editCommand;
            }
        }

        LoadCommand _loadCommand;
        /// <summary>
        /// Saves changes Command
        /// </summary>
        public ICommand LoadAction
        {
            get
            {
                if (_loadCommand == null)
                    _loadCommand = new LoadCommand(); // RelayCommand(param => this.SaveChanges(), param => this.CanSave);

                return _loadCommand;
            }
        }

        FilterCommand _filterCommand;
        /// <summary>
        /// Saves changes Command
        /// </summary>
        public ICommand FilterAction
        {
            get
            {
                if (_filterCommand == null)
                    _filterCommand = new FilterCommand(); 

                return _filterCommand;
            }
        }

        SetCommand setCommand;
        /// <summary>
        /// Saves changes Command
        /// </summary>
        public ICommand SetAction
        {
            get
            {
                if (setCommand == null)
                    setCommand = new SetCommand();

                return setCommand;
            }
        }
        

        EditSave _editSave;
        /// <summary>
        /// Saves changes Command
        /// </summary>
        public ICommand EditSaveAction
        {
            get
            {
                if (_editSave == null)
                    _editSave = new EditSave(); 

                return _editSave;
            }
        }

        
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string prop)
        {
            if( PropertyChanged != null )
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }

        #endregion
    }
}
